package cn.onlineclass.controller;

import cn.onlineclass.entity.OUnit;
import cn.onlineclass.service.OUnitService;
import cn.onlineclass.util.AjaxPage;
import cn.onlineclass.util.AjaxResult;
import cn.onlineclass.util.StringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author wang
 * @date 2022/3/11 9:16
 * @since 1.0
 */
public class OUnitController {
    public String selectAll(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        List<OUnit> p = OUnitService.INST.findALL();
        String str = JSON.toJSONString(p);
        response.getWriter().print(str);
        return null;
    }

    public AjaxPage search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"), 1);
//        boolean inuse = StringUtil.isTrue(request.getParameter("inuse"));
//        long unitid = Long.parseLong(request.getParameter("unitid"));
        AjaxPage p = OUnitService.INST.search(pageNo, 2, true, 2);
//        String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteMapNullValue,
//                SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNonStringValueAsString);
//        response.getWriter().print(str);
        return p;
    }

    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        String name = request.getParameter("name");
//        long unitid = Long.parseLong(request.getParameter("unitid"));
//        String descrption = request.getParameter("descrption");
//        boolean inuse = StringUtil.isTrue(request.getParameter("inuse"));
//        long viewers = Long.parseLong(request.getParameter("viewers"));
//        String imgsrc = request.getParameter("imgsrc");
        long level = 2;
        OUnit oUnit = new OUnit();
        oUnit.setName("web-jsp");
        oUnit.setDescription("学习web和jsp");
        oUnit.setLevel(level);
        oUnit.setInuse(true);
        oUnit.setMajorid(4);
        int num = OUnitService.INST.insertBook(oUnit);
        if (num > 0) {
            return AjaxResult.success("保存成功");
        }
        return AjaxResult.error("保存失败");
    }

    public AjaxResult get(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
//        int id = Integer.parseInt(request.getParameter("id"));
        OUnit oUnit = OUnitService.INST.get(10);
        response.getWriter().print(JSON.toJSON(oUnit));
        return null;
    }

    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        long id = 12;
        long level = 3;
        OUnit oUnit = new OUnit(id, "java-jsp", "学习java-jsp", level, true, 4);
//        oUnit.setId(id);
//        oUnit.setName("java-20");
//        oUnit.setUnitid(unitid);
//        oUnit.setDescrption("java-20");
//        oUnit.setInuse(true);
//        oUnit.setViewers(viewers);
//        oUnit.setImgsrc("bbbbbbbbbbb");

        int num = OUnitService.INST.updateById(oUnit);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }
}
